package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

import com.itheima.service.db.soul.OptinsService;
import com.itheima.service.db.soul.TitleService;
import com.itheima.soul.Options;
import com.itheima.soul.OptionsAll;
import com.itheima.soul.Title;
import com.itheima.soul.TitleVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class TitleManager {


    @DubboReference
    TitleService titleService;

    @DubboReference
    OptinsService optinsService;


    public ResponseEntity findTitleAll(Integer pageNum, Integer pageSize,Integer cid,String keyword) {


        PageBeanVo pageBeanVo = titleService.findTitleAll(pageNum, pageSize,cid,keyword);


        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findOptinsById(Long titleId) {

        List<Options> optionsList = optinsService.findByTitleId(titleId);
        TitleVo titleVo = null;
        if (CollUtil.isNotEmpty(optionsList)) {

            titleVo = new TitleVo();

            Title serviceById = titleService.findById(optionsList.get(0).getTitleId());
            titleVo.setId(titleId);
            titleVo.setOptionsText(optionsList);
            titleVo.setTitleName(serviceById.getTitle());
            titleVo.setCid(serviceById.getCid());

        }

        return ResponseEntity.ok(titleVo);
    }

    /**
     * 测试题的增加
     *
     * @param titleVo
     * @return
     */
    public ResponseEntity save(OptionsAll titleVo) {


        Title title = new Title();

        title.setTitle(titleVo.getTitleName());
        title.setCid(titleVo.getCid());
        title.setCreatedTime(DateUtil.formatTime(new Date()));

        titleService.save(title);

        Title byTitle = titleService.findByTitle(titleVo.getTitleName());

        if (StrUtil.isNotBlank(titleVo.getTitleA())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleA());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleB())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleB());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleC())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleC());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleD())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleD());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleE())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleE());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleF())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleF());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleG())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleG());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleH())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleH());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        if (StrUtil.isNotBlank(titleVo.getTitleI())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleI());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }

        if (StrUtil.isNotBlank(titleVo.getTitleJ())) {

            Options options = new Options();
            options.setTitleId(byTitle.getId());
            options.setOptions(titleVo.getTitleJ());
            options.setCreatedTime(DateUtil.formatTime(new Date()));
            optinsService.save(options);
        }
        //List<Options> optionsList = titleVo.getOptionsText();
        /*if (CollUtil.isNotEmpty(list)) {
            for (String options : list) {

                Options options1 = new Options();

                options1.setTitleId(byTitle.getId());
                options1.setCreatedTime(DateUtil.formatTime(new Date()));
                options1.setOptions(options);
                optinsService.save(options1);
            }
        }*/


        return ResponseEntity.ok("1");
    }

    /**
     * 测试题的修改
     *
     * @param titleVo
     * @return
     */
    public ResponseEntity update(TitleVo titleVo) {


        Title title = titleService.findById(titleVo.getId());

        title.setCid(titleVo.getCid());
        title.setTitle(titleVo.getTitleName());
        title.setUpdateTime(DateUtil.formatTime(new Date()));

        titleService.update(title);


        List<Options> optionsList = titleVo.getOptionsText();
        if (CollUtil.isNotEmpty(optionsList)) {
            for (Options options : optionsList) {
                options.setTitleId(title.getId());
                optinsService.update(options);
            }
        }


        return ResponseEntity.ok("1");
    }

    public ResponseEntity delete(Long id) {

        titleService.delete(id);
        optinsService.delete(id);



        return ResponseEntity.ok("1");
    }

    public ResponseEntity deletes(Integer[] id) {

        if (ArrayUtil.isNotEmpty(id)) {
            for (Integer aLong : id) {
                Long al = Long.valueOf(aLong);
                titleService.delete(al);
                optinsService.delete(al);

            }


        }


        return ResponseEntity.ok("1");
    }
}
